var mongo = require("mongodb");
var host = "localhost";
var port = mongo.Connection.DEFAULT_PORT;
var server = new mongo.Server(host, port, {auto_reconnect: true});
var db = new mongo.Db('test', server, {safe: true});


/*var mongo = require('mongodb');
var Db = require('mongodb').Db,
	Connection = require('mongodb').Connection,
	Server = require('mongodb').Server;
var db = new Db('test', new Server('localhost', Connection.DEFAULT_PORT), {safe: true});*/


db.open(function(err, db){
	if(err)
		throw err;
	else{
		console.log("connect success");

		db.collection('users', function(err, collection){
			collection.insert({username: 'kathy', firstname: '璇'}, function(err, docs){
				console.log(docs);
				db.close();
			});
		});
	}
});


db.on('close', function(err, db){
	if(err)
		throw err;
	else
		console.log("close success");
});


/*
	connect success
	[ { username: 'kathy',
	    firstname: '璇',
	    _id: 543a35c8dfe0d1e41f4d7d3d } ]
	close success
*/